package com.naiterui.ehp.bs.ds.modules.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.ds.common.utils.JwtUser;
import com.naiterui.ehp.bs.ds.modules.goods.entity.Warehouse;
import com.naiterui.ehp.bs.ds.modules.goods.mapper.WarehouseMapper;
import com.naiterui.ehp.bs.ds.modules.goods.service.IWarehouseService;
import com.naiterui.ehp.bs.ds.modules.goods.vo.WarehouseManageVO;
import com.naiterui.ehp.bs.ds.modules.goods.vo.WarehouseVO;
import com.naiterui.ehp.bs.ds.modules.sys.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    private final IUserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String getUserNameByWarehouseId(Long warehouseId) {
        return this.userService.getUserNameByWarehouseId(warehouseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateManage(Long warehouseId, WarehouseManageVO warehouseManage) throws BusinessException {
        this.userService.updateManage(warehouseId, warehouseManage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Long warehouseId, Integer status) {
        this.userService.updateStatus(warehouseId, status);
    }

    @Override
    public WarehouseVO getWarehouse() {
        JwtUser currentUser = SecurityUtils.getCurrentUser();
        Long warehouseId = currentUser.getWarehouseId();
        Preconditions.checkNotNull(warehouseId, "仓库ID不能为null");
        WarehouseVO warehouseVO = this.baseMapper.getWarehouseById(warehouseId);
        String address = warehouseVO.getAddress();
        warehouseVO.setAddress(address.replaceAll(",", ""));
        return warehouseVO;
    }
}
